/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package keshe;

import java.awt.event.*;
import java.sql.*;

import javax.swing.JOptionPane;

public class AddStudent extends StudentEdit implements ActionListener{

    public AddStudent(){
        super();
        b1.addActionListener(this);
        b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand()=="确定"){
            //计算总分
            //调用字符串转换浮点数方法
            float total=Float.parseFloat(chinese.getText())+Float.parseFloat(english.getText())+Float.parseFloat(maths.getText());

            try{
                //建立数据库连接
                Connection con=DataBaseCreate.getConnection();
                Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                //添加数据到数据库
                sql.executeUpdate("insert into student(Num,Name,Sex,Grade,Class,Major,Birth,Chinese,English,Maths,Total)" +
                        " values('"+num.getText()+"','"+name.getText()+"','"+s.getSelectedItem()+
                        "','"+grade.getSelectedItem()+"','"+clas.getSelectedItem()+"','"+major.getSelectedItem()+
                        "','"+birth.getText()+"',"+chinese.getText()+","+english.getText()+","+maths.getText()+","+total+")");
                //关闭数据库连接
                sql.close();
                con.close();
                JOptionPane.showMessageDialog(null, "成功录入学生信息与成绩", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
                setVisible(false);

            }catch(java.lang.ClassNotFoundException err){
                //捕捉错误，以对话框形式显示错误信息
                JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
                        JOptionPane.INFORMATION_MESSAGE);
            }catch (SQLException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if(e.getActionCommand()=="取消"){
            setVisible(false);
        }
    }

}

